/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */
package loginbean;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SearchWS
 */
public class SearchWS extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ResultSet rs;
		Statement st = null;
		Connection conn = null;
		DBManager db = new DBManager();
		ArrayList<loginbean.WebService> wsList= new ArrayList<loginbean.WebService>();
		String query = "select webserviceid,webservicename,webserviceurl,description,tag,category from mydb.webservicetable";
		String conditionString = null;
		int tenantId;
		String searchWords[];
		int i = 0;
		String searchString = request.getParameter("searchString");
		HttpSession session = request.getSession(true);
		
		if (!searchString.trim().equals("")) {
			searchWords = searchString.split(" ");
			if (searchWords.length == 1)
				conditionString = " where description like \'%" + searchString+"%\'";
			else {
				conditionString = " where description like ";
				while (i < searchWords.length - 1) {
					conditionString += "\'%"+searchWords[i++] + "%\' OR ";
				}
				conditionString +="\'%"+searchWords[i++]+"%\'";
			}
			query += conditionString;
		}
		try {
			conn = db.getDatabaseConnection();
			st = conn.createStatement();
			System.out.println("The search query for: " + query);
			rs = st.executeQuery(query);
			int count = 0;
			tenantId = Integer.parseInt(session.getAttribute("tenantid").toString());
			while (rs.next()) {
				wsList.add(new loginbean.WebService(tenantId,rs.getString(3),rs.getString(2),rs.getInt(1),rs.getString(4),rs.getString(5),rs.getString(6)));
										//WebService(int tenantID, String url, String name , int id, String tag, String description, String category)
				count++;
			}
			if (count > 0) {
				session.setAttribute("searchedWS", wsList);
				response.sendRedirect("searchedWebServices.jsp");
			} else {
				response.sendRedirect("error.jsp");
			}

			conn.close();
			System.out.println("Disconnected from database");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
